<?php

/**
 * ContactForm class.
 * ContactForm is the data structure for keeping
 * contact form data. It is used by the 'contact' action of 'SiteController'.
 */
class SshSetupForm extends CFormModel
{
	const USE_PASS = 0;
	const USE_KEY = 1;
	const SWITCH_USER_REQUIRED_OFF = 0;
	const SWITCH_USER_REQUIRED_ON = 1;
	
	public $id_account;
	public $ssh_port;
	public $pass_or_key = self::USE_PASS;
	public $user;
	public $pass;
	public $switch_user_required = self::SWITCH_USER_REQUIRED_OFF;
	public $su_user;
	public $su_pass;
	public $initial_prompt;
	public $pass_prompty;
//	public $public_key;
	public $privaty_key;
//	public $is_public_key_active_on_server;

	/**
	 * Declares the validation rules.
	 */
	public function rules()
	{
		return array(
			array('id_account, ssh_port, user, pass', 'required'),
			array('ssh_port', 'numerical', 'integerOnly'=>true),
			array('id_account, ssh_port, pass_or_key, user, pass, switch_user_required, su_user, su_pass, initial_prompt, pass_prompty, privaty_key', 'safe'),	// public_key, is_public_key_active_on_server 
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('su_user, su_pass, initial_prompt, pass_prompty', 'required', 'on'=>'switch_user_required'),
//			array('public_key', 'required', 'on'=>'use_key'),
		);
	}
	
	public function beforeValidate()
	{
		if($this->switch_user_required == self::SWITCH_USER_REQUIRED_ON){
			$this->setScenario('switch_user_required');
		}
		
		return parent::beforeValidate();
	}

	/**
	 * Declares customized attribute labels.
	 * If not declared here, an attribute would have a label that is
	 * the same as its name with the first letter in upper case.
	 */
	public function attributeLabels()
	{
		return array(
			'ssh_port' => 'SSH Port: ',
//			'public_key' => 'Copy SSH Public-Key to your server. ',
			'privaty_key' => 'RSA Private Key',
//			'is_public_key_active_on_server' => 'SSH Public-Key active on server. ',
			'user' => 'Username: ',
			'pass' => 'Password:',
			'switch_user_required' => 'Switch User Required ',
			'su_user' => 'su Username: ',
			'su_pass' => 'su Password: ',
			'initial_prompt' => 'Initial Prompt: ',
			'pass_prompty' => 'Password Prompt: ',
		);
	}
	
	public function getFieldLabel($field)
	{
		$labels = $this->attributeLabels();
		
		return empty($labels[$field])? '' : $labels[$field];
	}
	
	/*public function genSshpublicKey()
	{
		Yii::$enableIncludePath = false;
		require_once 'Crypt/RSA.php';
		$rsa = new Crypt_RSA();
		$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_OPENSSH);
		$rsaCreateResult = $rsa->createKey();
		
		$this->public_key = $rsaCreateResult['publickey'];
		$this->privaty_key = $rsaCreateResult['privatekey'];
	}*/
	
	public function setup()
	{
		include 'Net/SSH2.php';
		include 'Crypt/RSA.php';
		set_error_handler(function($errno, $errstr, $errfile, $errline){
			echo json_encode(array(
				'result' => false,
				'errors' => array(
					'ssh login' => array($errno.$errstr)
				)
			));
			exit();
		}, E_USER_NOTICE);
		
		$whmAccount = WhmAccount::model()->findByPk($this->id_account);
		if(empty($whmAccount)){
			$this->addError('Whmaccount', 'Ssh setup should base on a Whmaccount.');
			return false;
		}
		
		$host = '208.43.200.21';//$whmAccount->ipaddress . ':' . $this->ssh_port;
		$sshd2 = new Net_SSH2($host, 2087);
		
		if($this->pass_or_key == self::USE_PASS){
			if(!$sshd2->login($this->user, $this->pass)){
				$this->addError('ssh login', 'login failed by using Username and Password');
				return false;
			}
		}else{
			$key = new Crypt_RSA();
			$key->setPassword($this->pass);
			$key->loadKey($this->privaty_key);
			if (!$sshd2->login($this->user, $key)) {
			    $this->addError('ssh login', 'login failed by private key');
				return false;
			}
		}
		
		/*fdump($sshd2->exec('
			su root
			123456
		'));*/
		die();
		
		$whmaccount = WhmAccount::model()->findByPk($this->id_account);
		if($whmaccount->SshSetup){
			$sshSetup = $whmaccount->SshSetup;
		}else{
			$sshSetup = new SshSetup();
		}
		$sshSetup->attributes = $this->attributes;
		
		return $sshSetup->save(false);
		
	}
}